package com.wan37.logic.task.util;

import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.logic.task.domain.vo.TaskVO;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameTask;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-08-08 15:07
 **/
@Component
public class TaskRouteUtil {
    public void sendTaskResponse(int code, TaskVO taskVO) {
        GameTask.TaskResponse.Builder trb = GameTask.TaskResponse.newBuilder();
        trb.setCode(code);
        if (taskVO != null) {
            transferTaskVOToTask(trb, taskVO);
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                taskVO.getUserId(), PtoNum.TASKRESPONSE, trb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendTaskResponse(int code, String userId, List<TaskVO> taskVOList) {
        GameTask.TaskResponse.Builder trb = GameTask.TaskResponse.newBuilder();
        trb.setCode(code);
        if (taskVOList != null) {
            for (TaskVO taskVO : taskVOList) {
                transferTaskVOToTask(trb, taskVO);
            }
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.TASKRESPONSE, trb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void transferTaskVOToTask(GameTask.TaskResponse.Builder trb, TaskVO taskVO) {
        GameTask.Task.Builder tb = GameTask.Task.newBuilder();
        tb.setTaskId(taskVO.getTaskId());
        tb.setUserId(taskVO.getUserId());
        tb.setTaskParentType(taskVO.getTaskParentType());
        tb.setTaskSonType(taskVO.getTaskSonType());
        tb.setTotal(taskVO.getTotal());
        trb.addTask(tb.build());
    }
}
